import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ViewController,ToastController } from 'ionic-angular';
import { BaseUI } from '../../common/BaseUi';
import { LoadingController } from 'ionic-angular/components/loading/loading-controller';
import { RestProvider } from '../../providers/rest/rest';
import { Storage } from '@ionic/storage';

/**
 * Generated class for the LoginPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

// @IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage extends BaseUI {

  mobile: any;
  password: any;
  errorMessage: any;

  constructor(
    public navCtrl: NavController, 
    public viewCtrl: ViewController,
    public loadingCtrl: LoadingController,
    public toastCtrl: ToastController,
    public rest: RestProvider,
    public storage: Storage,
    public navParams: NavParams) {
      super(); //调用父类的构造函数
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad LoginPage');
  }

  login(){
    var loading = super.showLoading(this.loadingCtrl, '登录中...');
    this.rest.login(this.mobile, this.password)
      .subscribe(
        f => {
          if(f["Status"] == "OK"){
            //处理登录成功的逻辑
            this.storage.set('UserId',f["UserId"]);
            loading.dismiss();
            this.dismiss();
          }else{
            loading.dismiss();
            super.showToast(this.toastCtrl, f["StatusContent"]);
          }
        },
        error => this.errorMessage = <any>error
      );
  }

  /**
   * 关闭当前页面的方法
   * 
   * @memberof LoginPage
   */
  dismiss(){
    this.viewCtrl.dismiss();
  }

}
